//
//  ShopingCar.m
//  YaoHeAPP
//
//  Created by mac on 15/11/20.
//  Copyright (c) 2015年 mac. All rights reserved.

//*************************求购页面

#import "ShopingCar.h"
#import "Controller.h"
#import "SVPullToRefresh.h"
#import "ShopingListCell.h"
#import "shopImagecell.h"
#import "UIImageView+WebCache.h"
#import "shopDatamodel.h"
#import "shopmodellist.h"
#import "DetailesViewControl.h"
#import "Tool.h"
#import "pushDatamodel.h"
#import "labelview.h"
#import "CreateCompanyVC.h"
#import "TongYong.h"
#import "UMMobClick/MobClick.h"
#import "Tool.h"
#import "RootTabBarViewController.h"
#import "FindCompanyViewController.h"
#import "CompanyInviteCodeViewController.h"
#import "YongHuQuanXuanModel.h"
#import "MJExtension.h"

@interface ShopingCar ()<UITableViewDataSource,UITableViewDelegate,labelviewdelegate>
{
    UIImageView *_imageNavGood;
    UIButton * _buttonGood;
    UIButton * _buttonShop;

    UIRefreshControl *control;
    UISegmentedControl *segmentedControl;
    
    NSMutableArray *arrayList;//报价数组；
    
    NSMutableArray *otherarrlist;//其他数组
    
    BOOL image11;
    NSString *tapylabeltext;//暂无报价（条数）
    NSString *shopNamelable;//cell的主要名称
    NSString *statusName;//状态名称
    
    NSInteger segIndex;
    
    shopDatamodel *datamodel;
    shopmodellist *listdata;
    
    NSNumber *qoting;
    
    BOOL pushview;
    NSString *phushopID;
    
    NSInteger listTAPY;
    NSInteger num;//ADD数组的总数
    
    NSTimer *timer;
    
    
    UIView *tishiView;
    
    BOOL tim;
    
    labelview *la;//无数据显示的页面

    
}
@property(nonatomic,strong) UITableView *tableview;

@property (nonatomic, assign)NSInteger  type;//报价中10  剩下为其他

@property (nonatomic, assign)NSInteger  page;//页数


@end

@implementation ShopingCar

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"SHOPCAR_SHOW_NOW"];
    
//    [self.tableview triggerPullToRefresh];
    
   
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SHOPCAR_SHOW_NOW"];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"upview" object:nil];
    
    //取消定时器
    [timer invalidate];
    timer = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ShopingCar"];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ShopingCar"];
    
    //注册一个通知监听通知过来的操作
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(upview:) name:@"upview" object:nil];
    
    //判断是否有创建公司的权限
    [self panduanCreate];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(Action) userInfo:nil repeats:YES];

    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.hidden=YES;
    
    
    phushopID=@"1";
    
    segIndex=0;
    
    pushview=NO;
    
    arrayList=[[NSMutableArray alloc]init];
    
    otherarrlist=[[NSMutableArray alloc]init];
    
    
    UIView * topBagView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
    
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bg_user_center"]];
    topBagView.backgroundColor=bgColor;
   
    
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.frame=CGRectMake(0,20 , self.view.frame.size.width, 20);
    titleLabel.text=@"求购";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [topBagView addSubview:titleLabel];

    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"报价",@"其他",nil];
    //初始化UISegmentedControl
    segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(10, 45,self.view.frame.size.width-20, 29);
    segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引
    
    segmentedControl.tintColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0  alpha:0.2];

//    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    [segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    
    
    //修改字体的默认颜色与选中颜色
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:16.f],
                          NSForegroundColorAttributeName: [UIColor whiteColor]};
    [segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
    
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:16.f],
                                               NSForegroundColorAttributeName: [UIColor lightTextColor]};
    [segmentedControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

    segmentedControl.layer.masksToBounds=YES;
    segmentedControl.layer.cornerRadius=15;
    segmentedControl.layer.borderWidth=2;
//    segmentedControl.layer.borderColor=[UIColor colorWithRed:120/255.0 green:205/255.0 blue:226/255.0  alpha:1].CGColor;
    segmentedControl.layer.borderColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0  alpha:0.2].CGColor;

     [topBagView addSubview:segmentedControl];
    
    // 指定索引的选项.
    [segmentedControl setSelectedSegmentIndex:0];
    
    _type=10;
    _page=1;

    
    [self.view addSubview:topBagView];
    
   
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80-kTabBarHeight)];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];

    
    __weak ShopingCar *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
    // setup infinite scrolling
    [self.tableview addInfiniteScrollingWithActionHandler:^{
        [weakSelf insertRowAtBottom];
    }];

    [self insertRowAtTop];
    

}


//判断是否创建公司
-(void)panduanCreate
{
    [Controller requestWithURL:POSTCHECK jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            YongHuQuanXuanModel *yongHuQuanXuanModel=[YongHuQuanXuanModel mj_objectWithKeyValues:data.data];
            NSString *IDnumString=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.COMPANY_ID];
            if ([IDnumString isEqualToString:@"0"]) {
                NSString *statusSt=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.STATUS];
                if ([TongYong isBlankObjectAndStr:yongHuQuanXuanModel.STATUS]||[statusSt isEqualToString:@"-1"]){
                    //没有值便是 申请
                    //没有绑定公司 （提示是否加入公司  或者  创建公司  或者 输入验证码 加入公司）
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:@"您还没有“创建/加入”一家公司，不能进行该操作"
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:@"创建公司",@"查找公司",@"输入邀请码",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 0;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                }else{
                    //0申请中  -1被拒绝
                    
                    NSString *messageSt=[NSString stringWithFormat:@"您于%@申请加入“%@”，公司主管理员尚未同意您加入公司。",yongHuQuanXuanModel.APPLY_TIME,yongHuQuanXuanModel.APPLY_COMPANY_NAME];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:messageSt
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消申请"
                                                          otherButtonTitles:@"关闭",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 100;
                    //只读属性，看AlertView是否可见
                    [alert show];
                }
            
        }else{
            
        }
    }
    }];
    

}

//根据被点击按钮的索引处理点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==0) {
         if(buttonIndex==0){
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
            
        }else if (buttonIndex==1) {
            //创建公司
            CreateCompanyVC*creatVC=[CreateCompanyVC new];
            [self.navigationController pushViewController:creatVC animated:YES];

        }else if(buttonIndex==2){
            //查找公司
            FindCompanyViewController*findCompanyViewController=[FindCompanyViewController new];
            [self.navigationController pushViewController:findCompanyViewController animated:NO];
            
        }else if (buttonIndex==3){
            //输入邀请码
            CompanyInviteCodeViewController *companyInviteCodeViewController=[CompanyInviteCodeViewController new];
            [self.navigationController pushViewController:companyInviteCodeViewController animated:YES];
        }

    }else if(alertView.tag==100){
        if (buttonIndex==0) {
            //取消申请
            [self quxiaoshenqingBut];
        }else{
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
        }
        
    }
}
//取消加入公司申请
-(void)quxiaoshenqingBut{
    
    [Controller requestWithURL:PostCancelApply jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            ALERTSHOW(@"你已成功取消加入该公司的申请")
        }else{
            
            ALERTSHOW(data.message);
        }
    }];
    
}

- (void)willPresentAlertView:(UIAlertView *)alertView{
    
    for( UIView * view in alertView.subviews ){
        
        if( [view isKindOfClass:[UILabel class]] )
        {
            UILabel* label = (UILabel*) view;
            
            label.textAlignment = NSTextAlignmentLeft;
        }
        
    }
}


-(void)Action
{
    //增加判断，让定时器筛选小于0的时间值
    //进行过滤筛选
    NSInteger cou=arrayList.count;
    NSMutableArray *filArray=[NSMutableArray array];
    for (NSInteger i=0; i<cou; i++) {
        shopmodellist  *Slistdata=[[shopmodellist alloc]initWith:arrayList[i]];
        if ([Slistdata.total_seconds integerValue]<=0) {
            [filArray addObject:arrayList[i]];
        }
        
    }
    //通过过滤器过滤掉相同元素
    NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filArray];
    
    NSArray * filter = [arrayList filteredArrayUsingPredicate:filterPredicate];
    
    [arrayList removeAllObjects];
    
    [arrayList addObjectsFromArray:filter];
    
    if (filArray.count>0) {
        qoting=[NSNumber numberWithInteger:arrayList.count];
        NSString *baojianum=[NSString stringWithFormat:@"报价(%@)",qoting];
        //设置标题
        [segmentedControl setTitle:baojianum forSegmentAtIndex:0];
        
        //判断 是否  有 无数据
        if (arrayList.count==0) {
            if (la) {
                [la removeFromSuperview];
            }

            la=[[labelview alloc]init];
            [la setimage:@"dingdanNew" tit:@"您没有正在求购的询单数据" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
            la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 133);
            la.label.frame=CGRectMake(50, 193, self.view.frame.size.width-100, 30);
            la.button.frame=CGRectMake(40, 235, self.view.frame.size.width-80, 40);
            la.delegate=self;
            [_tableview addSubview:la];

            

        }else{
            
            if (la) {
                [la removeFromSuperview];
            }
            [self.tableview reloadData];
            
        }
        


    }
    
   

}




//相应观察者方法
-(void)upview:(NSNotification *)notification
{

    NSDictionary *dic = [notification object];//获取到传递的对象
    
    pushDatamodel *pushmodel=[[pushDatamodel alloc]initWith:dic];
    //推送的时间和列表刷新时间进行对比
    //是报价中就可以刷新
    if (segIndex==0&&pushmodel.happentime>[datamodel.refersh_time integerValue]) {
    
        
        if (pushmodel.type==0) {
            //求购审核通过（提示请求通过）
            
        }else if(pushmodel.type==1){
            //卖家忽略求购（）
        }else if(pushmodel.type==2){
            //新求购 调到求购列表 先判断类表中是否已有没有新加一条推送过来的消息并将其标记（边框）点击或刷新标记取消）
            NSDictionary *pushData=pushmodel.data;
            
            NSDictionary *pusarray=pushData[@"purchase"];
            
            shopmodellist *listdt=[[shopmodellist alloc]initWith:pusarray];
            phushopID=listdt.purchase_id;
            
            //将数组放进第一位
            [arrayList insertObject:pusarray atIndex:0];
            
            pushview=YES;
            if (la) {
                [la removeFromSuperview];
            }
            [self.tableview reloadData];
            
            NSInteger ppo=[qoting integerValue];
            
            if (ppo==3) {
                ppo=3;
            }else{
            ppo=ppo+1;
            }
            NSString *baojianum=[NSString stringWithFormat:@"报价(%ld)",(long)ppo];
            //设置标题
            [segmentedControl setTitle:baojianum forSegmentAtIndex:0];

            
            

        }else if(pushmodel.type==3){
            //"求购已取消"（将其在报价中的列表中移除）
            NSInteger  pushid=[pushmodel.data[@"purchaseID"] integerValue];
            NSInteger inted = 0;
           
            phushopID=[NSString stringWithFormat:@"%ld",(long)pushid];
            
            for (NSInteger i=0; i<arrayList.count; i++) {
              shopmodellist *listdata3=[[shopmodellist alloc]initWith:arrayList[i]];
                NSInteger  datapurid=[listdata3.purchase_id integerValue];
                if(pushid==datapurid){
                    inted=i;
                    [arrayList removeObjectAtIndex:inted];//按下标删除元素
                    pushview=YES;
                    
                    //判断 是否  有 无数据
                    if (arrayList.count==0) {
                        
                        la=[[labelview alloc]init];
                        [la setimage:@"dingdanNew" tit:@"您没有正在求购的询单数据" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                        la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                        la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 133);
                        la.label.frame=CGRectMake(50, 193, self.view.frame.size.width-100, 30);
                        la.button.frame=CGRectMake(40, 235, self.view.frame.size.width-80, 40);
                        la.delegate=self;
                        [_tableview addSubview:la];

                        
                        
                    }else{
                        
                        if (la) {
                            [la removeFromSuperview];
                        }
                        [self.tableview reloadData];
                        
                    }

                    NSInteger ppo=[qoting integerValue];
                    ppo=ppo-1;
                    NSString *baojianum=[NSString stringWithFormat:@"报价(%ld)",(long)ppo];
                    //设置标题
                    [segmentedControl setTitle:baojianum forSegmentAtIndex:0];
                }
            }
            

            
        }else if(pushmodel.type==4){
            //"求购有新报价"找到当前purchaseid的cell将几条报价加上去（并且标记））
            
            listTAPY=4;
            NSMutableArray *addarry=[NSMutableArray array];
            addarry=pushmodel.data[@"add"];
            num=addarry.count;
           
            NSInteger  pushid=[pushmodel.data[@"purchaseID"] integerValue];
            phushopID=[NSString stringWithFormat:@"%ld",(long)pushid];
            
            for (NSInteger i=0; i<arrayList.count; i++) {
                shopmodellist *listdata3=[[shopmodellist alloc]initWith:arrayList[i]];
                NSInteger  datapurid=[listdata3.purchase_id integerValue];
                if(pushid==datapurid){
                    pushview=YES;
                    //指定刷新固定cell
                    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:i inSection:0];
                    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
                    
                }
            }
            
            
        }else if(pushmodel.type==5||pushmodel.type==7||pushmodel.type==8||pushmodel.type==9){
            //指标表格一下
            //"求购报价已修改"（标记对应id的cell）
//            listTAPY=5;
            
            //找到固定cell的id
            NSInteger  pushid=[pushmodel.data[@"purchaseID"] integerValue];
            phushopID=[NSString stringWithFormat:@"%ld",(long)pushid];
            for (NSInteger i=0; i<arrayList.count; i++) {
                shopmodellist *listdata3=[[shopmodellist alloc]initWith:arrayList[i]];
                NSInteger  datapurid=[listdata3.purchase_id integerValue];
                if(pushid==datapurid){
                    pushview=YES;
                    //指定刷新固定cell
                    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:i inSection:0];
                    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
                    
                }
            }

            
        }else if(pushmodel.type==6){
            //"求购报价已取消"（标记）
            listTAPY=6;
            
            //找到固定cell的id
            NSInteger  pushid=[pushmodel.data[@"purchaseID"] integerValue];
            phushopID=[NSString stringWithFormat:@"%ld",(long)pushid];
            for (NSInteger i=0; i<arrayList.count; i++) {
                shopmodellist *listdata3=[[shopmodellist alloc]initWith:arrayList[i]];
                NSInteger  datapurid=[listdata3.purchase_id integerValue];
                if(pushid==datapurid){
                    pushview=YES;
                    //指定刷新固定cell
                    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:i inSection:0];
                    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationRight];
                    
                }
            }

        }
        
        
    }

}



- (void)insertRowAtTop {
    
    
    //请求条数
    [self posttoyalname];
    
     __weak ShopingCar *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    _page=1;
    NSNumber*number=[NSNumber numberWithInteger:_type];
    //其他
     if (segIndex==1) {
         NSNumber*number1=[NSNumber numberWithInteger:1];
         NSNumber*number2=[NSNumber numberWithInteger:10];
         NSDictionary*di=@{@"status":number,@"page_num":number1,@"page_step":number2};
         [Controller requestWithURL:PURCHASEINFO jsondic:di finishBlock:^(ClassData* data) {
                 if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
                 {
                         datamodel=[[shopDatamodel alloc]initWith:data.data];
                         [otherarrlist removeAllObjects];
         
                         [otherarrlist addObjectsFromArray:datamodel.purchase_infos];
                     
                     //判断 是否  有 无数据
                     if (otherarrlist.count==0) {
                         if (la) {
                             [la removeFromSuperview];
                         }
                         
                         la=[[labelview alloc]init];
                         [la setimage:@"dingdanNew" tit:@"您没有正在求购的询单数据" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                         la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                         la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 133);
                         la.label.frame=CGRectMake(50, 193, self.view.frame.size.width-100, 30);
                         la.button.frame=CGRectMake(40, 235, self.view.frame.size.width-80, 40);                         la.delegate=self;
                         [_tableview addSubview:la];


                         
                         
                         
                     }else{
                         
                         if (la) {
                             [la removeFromSuperview];
                         }
                         [self.tableview reloadData];
                         
                     }
                     
                }
                else
                {
                     
                }
            }];
      }else {
         //报价中
          NSDictionary*di=@{@"status":number};
          [Controller requestWithURL:PURCHASEINFO jsondic:di finishBlock:^(ClassData* data) {
              if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
              {
                  if (timer) {
                             //取消定时器
                             [timer invalidate];
                             timer = nil;
         
                             //    TimerInterval : 执行之前等待的时间。比如设置成1.0，就代表1秒后执行方法
                             //    target : 需要执行方法的对象。
                             //    selector : 需要执行的方法
                             //    repeats : 是否需要循环
         
                             timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(Action) userInfo:nil repeats:YES];
         
                         }else{
         
                             timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(Action) userInfo:nil repeats:YES];
         
                         }
         
         
                         datamodel=[[shopDatamodel alloc]initWith:data.data];
         
                         [arrayList removeAllObjects];
         
                         [arrayList addObjectsFromArray:datamodel.purchase_infos];
         
                         //进行过滤筛选
                         NSInteger cou=arrayList.count;
                         NSMutableArray *filArray=[NSMutableArray array];
                         for (NSInteger i=0; i<cou; i++) {
                         shopmodellist  *Slistdata=[[shopmodellist alloc]initWith:arrayList[i]];
                             if ([Slistdata.total_seconds integerValue]<=0) {
                                 [filArray addObject:arrayList[i]];
                             }
         
                         }
                         //通过过滤器过滤掉相同元素
                         NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filArray];
         
                         NSArray * filter = [arrayList filteredArrayUsingPredicate:filterPredicate];
         
                         [arrayList removeAllObjects];
         
                         [arrayList addObjectsFromArray:filter];
                         //复制数量
                         qoting=[NSNumber numberWithInteger:arrayList.count];
                         NSString *baojianum=[NSString stringWithFormat:@"报价(%@)",qoting];
                         //设置标题
                         [segmentedControl setTitle:baojianum forSegmentAtIndex:0];
                         
                  //判断 是否  有 无数据
                  if (arrayList.count==0) {
                      if (la) {
                          [la removeFromSuperview];
                      }
                      la=[[labelview alloc]init];
                      [la setimage:@"dingdanNew" tit:@"您没有正在求购的询单数据" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                      la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                      la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 133);
                      la.label.frame=CGRectMake(50, 193, self.view.frame.size.width-100, 30);
                      la.button.frame=CGRectMake(40, 235, self.view.frame.size.width-80, 40);                      la.delegate=self;
                      [_tableview addSubview:la];
                      
                  }else{
                      
                      if (la) {
                          [la removeFromSuperview];
                      }
                      [self.tableview reloadData];
                      
                  }
                     }
                 else
                 {
                 
                 }
             }];

      }
    
   // 3. 结束刷新(相当于动画)
  [weakSelf.tableview endUpdates];
    

  [weakSelf.tableview.pullToRefreshView stopAnimating];
}

-(void)didClickButtonInBottomView {
    RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
    [rootTabBarViewController changeRootViewControllerTo:2];
}
- (void)insertRowAtBottom {
    
    __weak ShopingCar *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    //是其他就可以刷新
    if (segIndex==1) {
       
        
        _page=_page+1;
        NSNumber*number=[NSNumber numberWithInteger:_type];
        NSNumber*number1=[NSNumber numberWithInteger:_page];
        NSNumber*number2=[NSNumber numberWithInteger:10];
        NSDictionary*di=@{@"status":number,@"page_num":number1,@"page_step":number2};
        
        [Controller requestWithURL:PURCHASEINFO jsondic:di finishBlock:^(ClassData* data) {
            if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
            {
                datamodel=[[shopDatamodel alloc]initWith:data.data];
                
                NSArray *arry=datamodel.purchase_infos;
                               
                [otherarrlist addObjectsFromArray:arry];
                
                [self.tableview reloadData];
                
            }
            else
            {
                
            }
        }];
        
        

    }
    
    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.infiniteScrollingView stopAnimating];
    
}


-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg
{
    segIndex = Seg.selectedSegmentIndex;
   
    //请求条数
    [self posttoyalname];

    
    if (segIndex==0) {
        _type=10;
        [otherarrlist removeAllObjects];
        [self.tableview reloadData];
        [self insertRowAtTop];

    }else{
    
        //取消定时器
        [timer invalidate];
        timer = nil;
        
        _type=-5;
        [arrayList removeAllObjects];
        [self.tableview reloadData];
       [self insertRowAtTop];
    
        
    }
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (segIndex==1) {
        return otherarrlist.count;
    }
    
    return arrayList.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"ShopingListCell";
    
    if (segIndex==1) {
         listdata=[[shopmodellist alloc]initWith:otherarrlist[indexPath.row]];
    }else{
    
         listdata=[[shopmodellist alloc]initWith:arrayList[indexPath.row]];
    
    }
    
    
     NSInteger Snum=[listdata.status  integerValue];
    
    //拼接大名称的字段
    
    //形状
    NSString *paihao;
    if ([TongYong isBlankObjectAndStr:listdata.TRADE_MARK ]) {
        
        paihao=@"";
        
    }else{
        
        paihao=[NSString stringWithFormat:@"/%@",listdata.TRADE_MARK];
        
    }

    
    
    shopNamelable=[NSString stringWithFormat:@"%@ %@ %@/%@ %@",listdata.variety_name,paihao,listdata.material_name,listdata.surface,listdata.spec_code];
    
    //判断数据中是否有图片数据
    if (listdata.imageurl.length!=0) {
        image11=YES;
    }else{
        image11=NO;
    }
    
   

    
    if (image11) {
     
        //自定义cell类有图片
        ShopingListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            //通过xib的名称加载自定义的cell
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopingListCell" owner:self options:nil] lastObject];
        }
        
        
        NSInteger Squote_num=[listdata.quote_num  integerValue];
        
        
        NSInteger seid=[listdata.purchase_id integerValue];
        NSInteger  puid=[phushopID integerValue];
        //通过pushview来判断是否是通知过来刷新的表格
        if (pushview&&seid==puid) {
            UIView *mog=[[UIView alloc]init];
            mog.backgroundColor=[UIColor clearColor];
            mog.frame=CGRectMake(2, 2, self.view.frame.size.width-4, 90);
            mog.layer.borderWidth=2;
            mog.layer.borderColor=[UIColor redColor].CGColor;
            [cell addSubview:mog];
            if (listTAPY==4) {
                //新报价
                Squote_num=Squote_num+num;
            }else if (listTAPY==6){
                //报价取消
                Squote_num=Squote_num-1;
            }
        }
        pushview=NO;

       
        
        //判断状态进行状态兑换
        if (Snum==-2) {
            cell.tapylable.text=@"已过期";
            cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_timeup"];
            
        }else if(Snum==-1)
        {
            cell.tapylable.text=@"无效";
            cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_timeup"];
            
        }else if(Snum==0)
        {
           cell.tapylable.text=@"待审核";
           cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_quotation"];
        }else if(Snum==10)
        {
            cell.tapylable.text=@"报价中";
            cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_quotation"];
        }else if(Snum==15)
        {
            cell.tapylable.text=@"已关闭";
            cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_cancel"];
        }else if(Snum==20)
        {
           cell.tapylable.text=@"待处理";
            cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_waiting"];
        }

        
        
        
        //根据报价条数确定是否暂无报价
        if (Squote_num==0) {
            //暂无报价
            tapylabeltext=@"暂无报价";
            cell.baojiaTapy.text=tapylabeltext;
            cell.baojiaTapy.textColor=[UIColor colorWithRed:148/255.0 green:148/255.0 blue:148/255.0  alpha:1];
            
        }else{
            tapylabeltext=[NSString stringWithFormat:@"%ld条报价",(long)Squote_num];
            cell.baojiaTapy.text=tapylabeltext;
            cell.baojiaTapy.textColor=[UIColor colorWithRed:255/255.0 green:100/255.0 blue:52/255.0  alpha:1];
        }
        
        cell.ordlistname.text=shopNamelable;
        cell.time.text=listdata.create_time;
        cell.addare.text=listdata.add;
        [cell.shopimage sd_setImageWithURL:[NSURL URLWithString:listdata.imageurl] placeholderImage:[UIImage imageNamed:@"ic_qiugou_quotation"]];
    
        return cell;
    }
//        //指定cellIdentifier为自定义的cell
//        static NSString *CellIdentifier = @"shopImagecell";
        //自定义cell类无图片
        shopImagecell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            //通过xib的名称加载自定义的cell
            cell = [[[NSBundle mainBundle] loadNibNamed:@"shopImagecell" owner:self options:nil] lastObject];
        }
    
    NSInteger Squote_num=[listdata.quote_num  integerValue];
    
    NSInteger seid=[listdata.purchase_id integerValue];
    NSInteger  puid=[phushopID integerValue];
    //通过pushview来判断是否是通知过来刷新的表格
    if (pushview&&seid==puid) {
        UIView *mog=[[UIView alloc]init];
        mog.backgroundColor=[UIColor clearColor];
        mog.frame=CGRectMake(2, 2, self.view.frame.size.width-4, 90);
        mog.layer.borderWidth=2;
        mog.layer.borderColor=[UIColor redColor].CGColor;
        [cell addSubview:mog];

        if (listTAPY==4) {
            //新报价
            Squote_num=Squote_num+num;
        }else if (listTAPY==6){
            //报价取消
            Squote_num=Squote_num-1;
        }
    }
    
    pushview=NO;
    
    
    
    
    
    
    
    //判断状态进行状态兑换
    if (Snum==-2) {
        cell.tapylable.text=@"已过期";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_timeup"];
        
    }else if(Snum==-1)
    {
        cell.tapylable.text=@"无效";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_timeup"];
        
    }else if(Snum==0)
    {
        cell.tapylable.text=@"待审核";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_quotation"];
    }else if(Snum==10)
    {
        cell.tapylable.text=@"报价中";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_quotation"];
    }else if(Snum==15)
    {
        cell.tapylable.text=@"已关闭";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_cancel"];
    }else if(Snum==20)
    {
        cell.tapylable.text=@"待处理";
        cell.tapyimage.image=[UIImage imageNamed:@"ic_qiugou_waiting"];
    }
    
    
    
    //根据报价条数确定是否暂无报价
    if (Squote_num==0) {
        //暂无报价
        tapylabeltext=@"暂无报价";
        cell.baojiaTapy.text=tapylabeltext;
        cell.baojiaTapy.textColor=[UIColor colorWithRed:148/255.0 green:148/255.0 blue:148/255.0  alpha:1];
        
    }else{
        tapylabeltext=[NSString stringWithFormat:@"%ld条报价",(long)Squote_num];
        cell.baojiaTapy.text=tapylabeltext;
        cell.baojiaTapy.textColor=[UIColor colorWithRed:255/255.0 green:100/255.0 blue:52/255.0  alpha:1];
    }
    
    cell.ordlistname.text=shopNamelable;
    cell.time.text=listdata.create_time;
    cell.addare.text=listdata.add;

    
    return cell;
    
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (segIndex==1) {
        
        listdata=[[shopmodellist alloc]initWith:otherarrlist[indexPath.row]];
        
    }else{
        
        listdata=[[shopmodellist alloc]initWith:arrayList[indexPath.row]];
        
    }
   
    NSString *pushID=listdata.purchase_id;
//呵呵呵
    //  进入详情页面
    DetailesViewControl*detailes=[DetailesViewControl new];
    detailes.purchaseID=pushID;
    [self.navigationController pushViewController:detailes animated:YES];

}

-(void)posttoyalname
{

    [Controller requestWithURL:TOTALNUM jsondic:nil finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            qoting =data.data[@"PURCHASE_QUOTING_NUM"];
            NSNumber *othernum=data.data[@"PURCHASE_OTHER_NUM"];
//            "PURCHASE_QUOTING_NUM":7,          //报价中
//            "PURCHASE_CONFIRM_NUM":0,          //待确认
//            "PURCHASE_OTHER_NUM":1               //其他
            
//            NSString *baojianum=[NSString stringWithFormat:@"报价(%@)",qoting];
            NSString *other=[NSString stringWithFormat:@"其他(%@)",othernum];
            //设置标题
//            [segmentedControl setTitle:baojianum forSegmentAtIndex:0];
            [segmentedControl setTitle:other forSegmentAtIndex:1];
            
        }
        else
        {
            
        }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
